package com.zzy.service.impl;


import com.zzy.service.RedisService;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import reactor.core.Exceptions;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;
import java.util.concurrent.TimeUnit;


@Service
public class RedisServiceImpl implements RedisService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    // 序列化的 数据
    private RedisTemplate redisTemplate;


    @Override
    public boolean isConnect() {

        try {

            //连接本地的 Redis 服务
            String url = "192.168.125.130";//localhost
            //Jedis jedis = new Jedis(url,6379);// http://192.168.125.130/
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            JedisPool jedisPool = new JedisPool(poolConfig,url, 6379, 100000,"zzy");//给redis设置了密码zzy
            //Jedis jedis = jedisPool.getResource();
            if(jedisPool!=null){
                return true;
            }else{
                return false;
            }


        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }



    }



    /**获取所有的key*/
    public List<String> getKeys(){
       Set<String> keySet = stringRedisTemplate.keys("*");//只能获取String的
       return new ArrayList<>(keySet);
    }
    /**1、根据key 取 String 值*/
    public String getValueString(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }
    /**2、根据key 取 List 值*/
    public List getValueList(String key){
        return stringRedisTemplate.opsForList().range(key, 0, -1);
    }
    /**2.1、根据key 取 List 值 范围取值 */
    public List getValueList(String key,Integer start,Integer end){
        if(start==null){
            start=0;
        }
        if(end==null){
            end = -1;
        }
        return stringRedisTemplate.opsForList().range(key, start, end);
    }
    /**3、根据key 取 Hash 值 */
    public Map getValueHash(String key){
        return stringRedisTemplate.opsForHash().entries(key);
    }
    /**4、根据key 取 Set 值 */
    public Set getValueSet(String key){
        return stringRedisTemplate.opsForSet().members(key);
    }
    /**5、根据key 取 zset 值 成员不能重复 */
    public Set getValueZSet(String key){
        return stringRedisTemplate.opsForZSet().range(key,0,-1);
    }
    /**5.1、根据key 取 zset 值 成员不能重复 */
    public Set getValueZSet(String key,Integer start,Integer end){
        if(start==null){
            start=0;
        }
        if(end==null){
            end = -1;
        }
        return stringRedisTemplate.opsForZSet().range(key,start,end);
    }

    /**1、根据key 写入 String 值*/
    public boolean setValueString(String key,String value){
        try{
            stringRedisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 1.1、根据key 写入 String 值
     *  设置 多少 second 秒 过期
     * */
    public boolean setValueString(String key,String value,Integer second){
        try{
            stringRedisTemplate.opsForValue().set(key,value,second, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    /**2、根据 key 写入 List 值*/
    public boolean setValueList(String key,List list){
        try{
            stringRedisTemplate.opsForList().leftPushAll(key,list);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    /**3、根据key 写入 Hash 值 */
    public boolean setValueHash(String key,String mapkey,String mapVal){
        try{
            stringRedisTemplate.opsForHash().put(key,mapkey,mapVal);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
    /**3.1、根据key 写入 Hash 值 */
    public boolean setValueHash(String key,Map map){
        try{
            stringRedisTemplate.opsForHash().putAll(key,map);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
    /**4、根据key 写入 Set 值 */
    public boolean setValueSet(String key,String[] obj){
        try{
            stringRedisTemplate.opsForSet().add(key,obj);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**5、根据key 写入 zset 值 成员不能重复 */
    public boolean setValueZSet(String key,Set set){
        try{
            stringRedisTemplate.opsForZSet().add(key,set);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**5.1、根据key 写入 zset 值 成员不能重复 排序 */
    public boolean setValueZSet(String key,String val,Integer order){
        try{
            stringRedisTemplate.opsForZSet().add(key,val,order);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**删除*/
    public boolean delete(String key) {
        try{
            stringRedisTemplate.delete(key);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    /** 移除 set中某个元素 */
    public Set removeSetCloum(String key,Object[] obj){
        stringRedisTemplate.opsForSet().remove(key,obj);
        return stringRedisTemplate.opsForSet().members(key);
    }










    public String test(){

        Set<String> keySet = stringRedisTemplate.keys("*");//只能获取String的
        for(String s : keySet){
            if(s.contains("list")){
                List<String> oowwoo = stringRedisTemplate.opsForList().range(s, 0, -1);
                System.out.println(oowwoo);
                Iterator<String> it = oowwoo.iterator();
                System.out.println(s+":");
                while (it.hasNext()){
                    System.out.println(it.next());
                }

            }else{
                System.out.println(s+":"+stringRedisTemplate.opsForValue().get(s));
                System.out.println(redisTemplate.opsForValue().get("zzy"));
            }
        }



/*//清空
                while (redisTemplate.opsForList().size("oowwoo") > 0){
                    redisTemplate.opsForList().leftPop("oowwoo");
                }
//存储
                redisTemplate.opsForList().rightPushAll("oowwoo", list);*/


        //redisTemplate.opsForValue().set("zzy","hello world");
        //System.out.println(redisTemplate.opsForValue().get("zzy"));
        // System.out.println(redisTemplate.opsForValue().get("site-list"));

    return "这是测试方法";
    }
}

